//工具类

//---------------时间---------------------------------
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
/**
 * 获取当前日期
 */
function currentDate() {
  let date = new Date();
  let line = '-';
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  if (month >= 1 && month <= 9) {
    month = '0' + month;
  }
  if (day >= 0 && day <= 9) {
    day = '0' + day;
  }
  return year + line + month + line + day;
}
/**
 * 格式化日期格式
 */
function formateDateStr(year, month, day) {
  let line = '-';
  if (parseInt(month) >= 1 && parseInt(month) <= 9) {
    month = '0' + month;
  }
  if (parseInt(day) >= 0 && parseInt(day) <= 9) {
    day = '0' + day;
  }
  return year + line + month + line + day;

}
/**
 * 时间字符串
 */
function formateTimeStr(hour, min, sec) {
  let dot = ':';
  return hour + dot + min + dot + sec;
}

//-------------------多次共用方法-----------------------------
/**
 * 弹出提示
 */
function showToast(msg, time, icon, callBack) {
  wx.showToast({
    title: msg,
    icon: icon || 'none',
    duration: time || 1500,
    mask: true,
    success: function(res) {
      return typeof callBack == 'function' && callBack(res);
    }
  })
}

/**
 * 验证身份证号
 */
function testIdCard(idNo) {
  var regIdNo = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  if (!regIdNo.test(idNo)) {
    return false;
  }
  return true
}
/**
 * 验证邮箱
 */
function testEmile(emialAddress){
  var re = /^[A-Za-z\d]+([-_.][A-Za-z\d]+)*@([A-Za-z\d]+[-.])+[A-Za-z\d]{2,4}$/;
  if (!re.test(emialAddress)) {
    return false;
  } else {
    return true
  }
}
/**
 * 验证手机号
 */
function testPhone(phoneNum) {
  var myreg = /^[1][2,3,4,5,6,7,8,9][0-9]{9}$/;
  if (!myreg.test(phoneNum)) {
    return false;
  } else {
    return true;
  }
}
/**
 * 验证姓名
 */
function testName(name) {
  var nameReg = /^[\u4E00-\u9FA5\uf900-\ufa2d·s]{2,20}$/;
  if (!nameReg.test(name)) {
    return false;
  } else {
    return true;
  }
}
/**
 * 预览图片
 */
function previewImg(imgArry, currImg) {
  wx.previewImage({
    current: currImg,
    urls: imgArry,
  })
}
//复制
function copyText(e) {
  let copyContent = e.currentTarget.dataset.copytext;
  wx.setClipboardData({ //设置系统剪贴板的内容
    data: copyContent,
    success: function(res) {
      wx.getClipboardData({ //系统获取剪切板内容
        success: function(res) {
          console.log(res.data)
        }
      })
    },
  })
}

// loadding框
function showLoading(title, callBack) {
  wx.showLoading({
    title: title,
    mask: true,
    success: function(res) {
      return typeof callBack == 'function' && callBack(res)
    },
    fail: function(res) {
      return typeof callBack == 'function' && callBack(false)
    }
  })
}
// 判断有无中文
function textNoCharCode(text) {
  var re = /^[A-Za-z0-9]*$/;
  if (re.test(text)) return true;
  return false;
}

module.exports = {
  formatTime: formatTime,
  showToast: showToast,
  currentDate: currentDate,
  formateDateStr: formateDateStr,
  formateTimeStr: formateTimeStr,
  testIdCard: testIdCard,
  testPhone: testPhone,
  testName: testName,
  previewImg: previewImg,
  copyText: copyText,
  showLoading: showLoading,
  textNoCharCode: textNoCharCode,
  testEmile: testEmile
}

/**
 * 定义节流器
 * @fn 执行函数
 * @time 节流时间
 */

module.exports.throttle = function (fn, delay,parm) {
  clearTimeout(fn.timer);
  fn.timer = setTimeout(()=>{
    if (parm) fn(parm)
    else fn()
  },delay)
}